/**
 * 0000 0000
 *  ie Safari opera Firefox Chrome
 * @returns
 */
export function browser() {
    // 取得浏览器的userAgent字符串
    let userAgent = navigator.userAgent;
    let result = 0;
    let isIE = window.ActiveXObject !== undefined && userAgent.includes("MSIE");
    let isFirefox = userAgent.includes("Firefox");
    let isOpera = window.opr !== undefined;
    let isChrome = userAgent.indexOf("Chrome") && window.chrome;
    let isSafari =
        userAgent.includes("Safari") && userAgent.includes("Version");
    result |= isChrome ? 1 : 0;
    result |= isFirefox ? 2 : 0;
    result |= isOpera ? 4 : 0;
    result |= isSafari ? 8 : 0;
    result |= isIE ? 16 : 0;
    return result;
}
// 点赞
export function giveLike(params, errMsg, axios) {
    return put("/course/interact-comment/comment-like", params, errMsg, axios);
}
// 切割文件问
export function fileNameSplit(fileName) {
    if (fileName) {
        if (fileName.lastIndexOf(".") === -1) {
            return fileName;
        }
        return fileName.substring(0, fileName.lastIndexOf("."));
    }
    return null;
}
export function fileType(fileName) {
    if (!fileName || fileName.lastIndexOf(".") === -1) {
        return "unknow";
    }
    return fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
}
export function finalFileType(afterType) {
    if (!afterType) return "unknow";
    const type = {
        video: ["mp4", "m3u8", "flv", "mov", "mkv", "avi"],
        word: ["docx", "dox"],
        excel: ["xlsx", "xls"],
        pdf: ["pdf"],
        ppt: ["ppt"],
        img: ["png", "jpg", "image/jpeg", "image/jpg", "image/png"],
        zip: ["zip", "rar"],
        link: ["link"]
    };
    for (let key of Object.keys(type)) {
        if (type[key].includes(afterType.toLowerCase())) {
            return key;
        }
    }
    return "unknow";
}
export function getPlayInfoByResourceId(params, errMsg, axios) {
    return get(
        "/course/resources/record/get-play-info-resource-id",
        params,
        errMsg,
        axios
    );
}
// 获取视频播放记录
// export function lastPlayedTime(params, errMsg, axios) {
//     return get(
//         "/course/resources/watch/last-played-time",
//         params,
//         errMsg,
//         axios
//     );
// }

export const VueComponents = {
    computed: {
        onlyMember() {
            return (
                this.userLoginState &&
                !this.haveTeacherRule &&
                !this.isGroupAdmin
            );
        },
        onlyAssistant() {
            return (
                this.userLoginState && this.haveTeacherRule && !this.isTeacher
            );
        }
    },
    mounted() {
        window.onscrollMap = window.onscrollMap || {};
    },
    methods: {
        go(url) {
            const {
                path
            } = this.$route;
            if (url !== path) {
                this.$router.push(url);
            }
        },
        goBack() {
            this.$router.go(-1);
        },
        remToPx(rem) {
            if (process.client && window) {
                const size = getComputedStyle(document.body).fontSize.replace(
                    "px",
                    ""
                );
                return Number(size) * rem;
            } else {
                return 24 * rem;
            }
        },
        coverImage(path) {
            if (!path) {
                return "";
            } else if (path.startsWith("http")) {
                return path;
            }
            const state = this.state || this.$store.state;
            const systemImageConfig = state.systemImageConfig;
            for (const vl of systemImageConfig) {
                let result = false;
                (vl.imagePrefix || []).forEach((prefix) => {
                    if (!result) {
                        result = path.startsWith(prefix);
                    }
                });
                if (result) {
                    return result ? vl.imagesHost + path : path;
                }
            }
            return path;
        },
        flagPromise(fetchPromise, flag) {
            if (flag) {
                this.flag[flag] = true;
            }
            return fetchPromise
                .then((r) => r)
                .finally(() => {
                    if (flag) {
                        this.flag[flag] = false;
                    }
                });
        },
        memberHeader(sex, src) {
            if (src) return this.coverImage(src);
            return sex === "boy" ?
                "https://cdncashi.langrundata.com/VipLearningPlatform/%E6%A4%AD%E5%9C%86%E5%BD%A2%402x%20%281%29.png" :
                "https://cdncashi.langrundata.com/VipLearningPlatform/%E6%A4%AD%E5%9C%86%E5%BD%A2%402x.png";
        },
        toLogin() {
            this.$EventBus.$emit("login-register");
        }
    }
};
